﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Xml;
using Microsoft.BizTalk.Bam.EventObservation;
using Microsoft.BizTalk.Edi.Shared;

namespace Locallab.Ack999Helper.BusinessContextReader
{
    public class Utility
    {
        public static string[] SplitSegment(string strData, string strElementDelimiter)
        {
            var strArrayElements = strData.Split(strElementDelimiter.ToCharArray());

            

            return strArrayElements;
        }

        public static DirectEventStream GetEventStream()
        {
            return new DirectEventStream(DbUtils.BamPrimaryImportConnectionString, 1);
        }

        public static XmlDocument GetContextMessage(string strFileID, string strGS08, int nInterChangeControlNumber, int nFunctionalGroupNumber)
        {
            var xmlDoc = new XmlDocument();

            var sb = new StringBuilder();

            sb.Append("<ns0:Context xmlns:ns0=\"http://Locallab.Ack999Helper.Schemas.ContextProperty\">");

            sb.AppendFormat("<{0}>{1}</{0}>", new object[] { "FileID", strFileID });            
            sb.AppendFormat("<{0}>{1}</{0}>", new object[] { "GS08", strGS08 });
            sb.AppendFormat("<{0}>{1}</{0}>", new object[] { "InterChangeControlNumber", nInterChangeControlNumber });
            sb.AppendFormat("<{0}>{1}</{0}>", new object[] { "FunctionalGroupNumber", nFunctionalGroupNumber });
            sb.Append("</ns0:Context>");

            xmlDoc.LoadXml(sb.ToString());

            return xmlDoc;
        }

        public static int GetInterChangeControlNumber(string strData)
        {            
            var chArrayTemp = strData.ToCharArray();
            var strElementDelimiter = Convert.ToString(chArrayTemp[3]);
            var strArrayElements = SplitSegment(strData, strElementDelimiter);

            if (strArrayElements.Length > 13)
                return Convert.ToInt32(strArrayElements[13]);
            else
                return 0;            
        }


        public static int GetFunctionalControlNumber(string strData)
        {
            var chArrayTemp = strData.ToCharArray();
            var strElementDelimiter = Convert.ToString(chArrayTemp[2]);
            var strArrayElements = SplitSegment(strData, strElementDelimiter);

            if (strArrayElements.Length > 6)
                return Convert.ToInt32(strArrayElements[6]);
            else
                return 0;
        }

    }


    public class MapHelper
    {
        public static string GetAK301(string strElementName)
        {
            if (IsLoopSegment(strElementName))
                return strElementName.Substring(0, strElementName.IndexOf("_"));
            else
                return strElementName;
        }

        public static bool IsLoopSegment(string strElementName)
        {
            if (strElementName.IndexOf("_") > 0)
                return true;
            else
                return false;
        }

        public static string GetAK304(string strAK301, string strAK304)
        {
            if (IsLoopSegment(strAK301))
                return "I7";
            else
                return strAK304;
        }
    }
}
